# -*- coding: UTF-8 -*-
import md5
import os
import datetime

from pyramid.view import view_config
from pyramid.renderers import render_to_response
from pyramid.httpexceptions import HTTPFound
from sqlalchemy import and_, or_, desc

from pyramid.security import (
        remember,
        forget,
        IAuthenticationPolicy,
        )

from . import tz
from ..models.meta import (
    DBSession,
    )

from ..models.models import (
    Coupon,
    Product,
    User,
    Role
    )

from ..freedom.dateutil.parser import parse


@view_config(route_name='index')
@view_config(route_name='index1')
def index(request):
    tmpl_context = {}
    #get coupons
    now = datetime.datetime.now(tz)
    coupon_q = DBSession.query(Coupon).filter(Coupon.startdate<now).filter(Coupon.status==3).filter(Coupon.enddate>now).order_by(desc(Coupon.startdate))
    coupons = coupon_q[0:2]
    tmpl_context['coupons'] = coupons

    #get products
    product_q = DBSession.query(Product).filter(Product.enable==True).filter(Product.deleted==False).order_by(desc(Product.recommended))
    products = product_q[0:8]
    tmpl_context['products'] = products

    return render_to_response('/front/index.html', tmpl_context, request = request)
